from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from .models import Question
import datetime

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    output = ', '.join([q.question_text for q in latest_question_list])
    return HttpResponse(output)

# Leave the rest of the views (detail, results, vote) unchanged

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

# def g(request):
#     g = 'a'
#     if request.method == 'POST' and request.POST:
#         g = request.POST.get('g')
#         print(g)
#         Question.objects.create(
#             question_text = g,
#             pub_date = datetime.datetime.now()
#         )
#         userlist = Question.objects.all()
#         Question().save()
#     return render(request, "g.html",locals())


def g(request):
    if request.method == 'POST' and request.POST:
        Question.objects.all()
        obj = Question.objects.create(
            question_text = request.POST.get('position'),
            question_accuracy = request.POST.get('accuracy'),
            question_location = request.POST.get('location'),
            question_info = request.POST.get('info'),
            question_formattedAddress = request.POST.get('formattedAddress'),
            question_address = request.POST.get('address'),
            pub_date = datetime.datetime.now()
        )
        obj.save()
    return render(request, "g.html",locals())
